package com.constituteRelation.service;

import com.constituteRelation.model.Address;
import com.constituteRelation.model.Customer;
import com.utils.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class BusinessService {

    private HibernateUtil hibernateUtil = new HibernateUtil();
    private SessionFactory sessionFactory = hibernateUtil.getSessionFactory();

    private void saveCustomer() {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            Customer customer = new Customer();
            Address homeAddress = new Address("province1", "city1", "street1", "100001", customer);
            Address comAddress = new Address("province2", "city2", "street2", "200002", customer);
            customer.setName("Tom");
            customer.setHomeAddress(homeAddress);
            customer.setComAddress(comAddress);

            session.save(customer);

            tx.commit();

        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    private void saveAddressSeparately() {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            Address address = new Address("province1", "city1", "street1", "100001", null);
            session.save(address);

            tx.commit();

        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
    }

    private void saveCustomerWithNoAddress() {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            Customer customer = new Customer("Mike", new Address(null, null, null, null, null), null);

            session.save(customer);
            tx.commit();

        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
    }

    private Customer findCustomer(long customer_id) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Customer customer = (Customer) session.get(Customer.class, customer_id);
            System.out.println("Customer: " + customer.toString());
            tx.commit();

            return customer;
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
                System.out.println("");
            }
            throw e;
        } finally {
            session.close();
        }
    }

    private void printCustomerAddress(Customer customer) {
        Address homeAddress = customer.getHomeAddress();
        Address comAddress = customer.getComAddress();
        if (homeAddress == null) {
            System.out.println("Home Address of " + customer.getName() + " is null.");
        } else {
            System.out.println("Home Address of " + customer.getName() + " is: "
                    + homeAddress.getProvince() + " " + homeAddress.getCity() + " " + homeAddress.getStreet());
        }
        if (comAddress == null) {
            System.out.println("Company Address of " + customer.getName() + " is null.");

        } else {
            System.out.println("Company Address of " + customer.getName() + " is: "
                    + comAddress.getProvince() + " " + comAddress.getCity() + " " + comAddress.getStreet());
        }
    }
    private void deleteCustomer(Customer customer) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            session.delete(customer);
            tx.commit();
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    private void test() {
        saveCustomer();
//        saveAddressSeparately();//由于Address没有映射文件，所以不能单独操作
//        saveCustomerWithNoAddress();
        Customer customer = findCustomer(1);
        printCustomerAddress(customer);
        customer = findCustomer(2);
        printCustomerAddress(customer);
        deleteCustomer(customer);
        sessionFactory.close();
    }

    public static void main(String args[]) {
        new BusinessService().test();
    }
}